<?php
/**
 * CodeIgniter
 * 
 * @category	Loader
 * @package		dayscript.loader.adapter
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-13 $
 */

namespace dayscript\loader\adapter;

/**
 * CodeIgniter
 * 
 * Library auto loader for CodeIgniter framework
 * Be warned that CodeIgniter requires some global functions an global constants defined before instantiate any class or interface.
 * Knowed: log_message(...), get_instance(...), BASEPATH
 * 
 * 
 * @category	Loader
 * @package		dayscript.loader.adapter
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\loader\Loader
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-01 $
 */
class CodeIgniter extends \dayscript\loader\Loader	{
	/**
	 * CodeIgniter constructor
	 * @param string $name Loader identifier
	 * @param string $libraryDir Directory where the CodeIgniter framework is placed with a trailing DIRECTORY_SEPARATOR.
	 * @return CodeIgniter
	 */
	public function __construct( $name, $libraryDir )	{
		if( !\function_exists( 'log_message' ) )
			throw new \dayscript\Exception( 'CodeIgniter function "log_message( $type, $msg )" is not defined but required by de CI Framework.' , \E_USER_ERROR );
		if( !\function_exists( 'get_instance' ) )
			\trigger_error( 'CodeIgniter function "get_instance( )" and some times is required by de CI Framework.', \E_USER_WARNING );
		if( !\defined( 'BASEPATH' ) )
			\define( 'BASEPATH', __DIR__ . DIRECTORY_SEPARATOR );
		parent::__construct( $name, $libraryDir, '.php' );
	}
	/**
	 * {@inheritdoc }
	 */
	public function canLoadClass( $className )	{
		return ( \strpos( $className, 'CI_' ) === 0  && \file_exists( $this->libraryDir . \DIRECTORY_SEPARATOR . \strtolower( \str_replace( 'CI_', '', $className ) ) . $this->extension ) );
	}
	/**
	 * {@inheritdoc }
	 */
	public function loadClass( $className )	{
		if( \strpos( $className, 'CI_' ) === 0 )
			require( $this->libraryDir . \DIRECTORY_SEPARATOR . \strtolower( \str_replace( 'CI_', '', $className ) ) . $this->extension );
	}
}